package com.general.common.utils;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

import java.io.IOException;

/**
 * @author SongJiuHua.
 * @date Created on 2018/3/12.
 * @description
 */
public class JsonMapper {
	private static ObjectMapper mapper = new ObjectMapper();
	static{
		mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);  
		mapper.getDeserializationConfig()
		.set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
	}

	public static ObjectMapper getInstance(){
		return mapper;
	}

	public static String convertObjectToJson(Object object){
		try {
			return JsonMapper.getInstance().writeValueAsString(object);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
